<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="billcode">
        <el-input
          v-model="queryParams.billcode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientname">
        <el-input
          v-model="queryParams.clientname"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="daterangeDates"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsname">
        <el-input
          v-model="queryParams.goodsname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品批号" prop="batchcode">
        <el-input
          v-model="queryParams.batchcode"
          placeholder="请输入商品批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="摘要" prop="summaries">
        <el-input
          v-model="queryParams.summaries"
          placeholder="请输入摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['flow:flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flow:flow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flow:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['flow:flow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="单据编号" align="center" prop="billcode" />
      <el-table-column label="供应商" align="center" prop="supplyname" />
      <el-table-column label="客户名称" align="center" prop="clientname" />
      <el-table-column label="日期" align="center" prop="dates" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dates, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品编号" align="center" prop="goodscode" />
      <el-table-column label="商品名称" align="center" prop="goodsname" />
      <el-table-column label="商品规格" align="center" prop="goodsspec" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="产地" align="center" prop="place" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="商品批号" align="center" prop="batchcode" />
      <el-table-column label="生产日期" align="center" prop="producedate" />
      <el-table-column label="有效期至" align="center" prop="valdate" />
      <el-table-column label="摘要" align="center" prop="summaries" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flow:flow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flow:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商流向(批发)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="entname">
          <el-input v-model="form.entname" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplycode">
          <el-input v-model="form.supplycode" placeholder="请输入供应商编号" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplyname">
          <el-input v-model="form.supplyname" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="单据编号" prop="billcode">
          <el-input v-model="form.billcode" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="客户编号" prop="clientcode">
          <el-input v-model="form.clientcode" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="客户名称" prop="clientname">
          <el-input v-model="form.clientname" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="日期" prop="dates">
          <el-date-picker clearable
            v-model="form.dates"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品编号" prop="goodscode">
          <el-input v-model="form.goodscode" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="form.goodsname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsspec">
          <el-input v-model="form.goodsspec" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="产地" prop="place">
          <el-input v-model="form.place" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="商品批号" prop="batchcode">
          <el-input v-model="form.batchcode" placeholder="请输入商品批号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="producedate">
          <el-input v-model="form.producedate" placeholder="请输入生产日期" />
        </el-form-item>
        <el-form-item label="有效期至" prop="valdate">
          <el-input v-model="form.valdate" placeholder="请输入有效期至" />
        </el-form-item>
        <el-form-item label="摘要" prop="summaries">
          <el-input v-model="form.summaries" placeholder="请输入摘要" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow } from "@/api/flow/flow";

export default {
  name: "Flow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商流向(批发)表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 摘要时间范围
      daterangeDates: [],
      //日期可选范围
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.selectDate = minDate.getTime();
          if (maxDate) {
            this.selectDate = "";
          }
        },
        disabledDate: (time) => {
          if (this.selectDate !== "") {
            const one = 60 * 24 * 3600 * 1000;
            const minTime = this.selectDate - one;
            const maxTime = this.selectDate + one;
            return time.getTime() < minTime || time.getTime() > maxTime;
          }
        },
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billcode: null,
        clientname: null,
        dates: null,
        goodsname: null,
        batchcode: null,
        summaries: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  watch:{
    daterangeDates(){
      if(this.daterangeDates){
        if(this.daterangeDates.length==0){
          this.datatimerq()
        }
      }
      if(this.daterangeDates==null){this.datatimerq()}//当日期数据被清空时,读取日期计算事件
    }
  },
  created() {
    this.datatimerq()
    this.getList();
  },
  methods: {
    //日期计算-上个月1号至当前日
    datatimerq(){
      let dateTimes = new Date();//当前日期
      let nian=dateTimes.getFullYear()//当前年
      let yue=dateTimes.getMonth()+1//当前月
      let ri=dateTimes.getDate()//当前日
      if(yue<10){yue='0'+yue}
      if(ri<10){ri='0'+ri}
      this.daterangeDates=[nian+'-'+yue+'-'+'01',nian+'-'+yue+'-'+ri]
    },
    /** 查询供应商流向(批发)列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDates && '' != this.daterangeDates) {
        this.queryParams.params["beginDates"] = this.daterangeDates[0];
        this.queryParams.params["endDates"] = this.daterangeDates[1];
      }
      listFlow(this.queryParams).then(response => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entname: null,
        supplycode: null,
        supplyname: null,
        billcode: null,
        clientcode: null,
        clientname: null,
        dates: null,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        unit: null,
        place: null,
        num: null,
        price: null,
        amount: null,
        batchcode: null,
        producedate: null,
        valdate: null,
        summaries: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.datatimerq()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDates = [];
      this.resetForm("queryForm");
      this.datatimerq()
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商流向(批发)";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商流向(批发)";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商流向(批发)编号为"' + ids + '"的数据项？').then(function() {
        return delFlow(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flow/flow/export', {
        ...this.queryParams
      }, `flow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
